package com.dure.hotmemory.notification;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.text.TextUtils;


import com.dure.hotmemory.config.Constants;

import java.io.File;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * Created by dure on 2017/4/11.
 */

public class MyBroadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //接收广播：设备上新安装了一个应用程序包后自动启动新安装应用程序。  
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {

            String packageName = intent.getDataString().substring(8);
            if (TextUtils.equals(context.getPackageName(), packageName)) {
                // 清除下载完成通知， 删除安装包
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
                notificationManager.cancel(Constants.FASTREAD_DOWNLOAD_INDEX);

                File apkFile = new File(Environment.getExternalStorageDirectory(), "app/download/fastread.apk");
                if (apkFile.exists())
                    apkFile.delete();
            } else if (TextUtils.equals("com.dure.relationship", packageName)) {
                // 清除下载完成通知， 删除安装包
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
                notificationManager.cancel(Constants.RELATIONSHIP_DOWNLOAD_INDEX);

                File apkFile = new File(Environment.getExternalStorageDirectory(), "app/download/relationship.apk");
                if (apkFile.exists())
                    apkFile.delete();
            } else if (TextUtils.equals("com.dure.hdb", packageName)) {
                // 清除下载完成通知， 删除安装包
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
                notificationManager.cancel(Constants.HOTBREATH_DOWNLOAD_INDEX);

                File apkFile = new File(Environment.getExternalStorageDirectory(), "app/download/hotbreath.apk");
                if (apkFile.exists())
                    apkFile.delete();
            }
        }
    }
}
